package server;

import java.io.DataOutputStream;
import java.io.IOException;

import entry.Entry;
import event.Event;
import event.EventHandler;

public class FileRequestEventHandler implements EventHandler {

	@Override
	public void handle(Event e) {
		if (e.type == Event.FIL_TYPE) {
			String file = e.parameters;

			Model model = Model.getInstance();
			Entry entry = model.items.get(file);

			if (entry != null) {
				DataOutputStream os = e.getOutputStream();

				String host = entry.key;

				try {
					os.writeInt(Event.HOST_TYPE);
					os.writeLong(0);
					os.writeLong(0);
					os.writeInt(0);
					os.writeUTF(host + "DELIMETER" + entry.value);

					System.out.println("> file request - " + host + " has " + file);

					return;
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
			System.out.println("> file request - file not found");
		}
	}

}
